//
//  AvatarChooseView.swift
//  ShouChong
//
//  Created by 陈友文 on 2024/6/17.
//

import SwiftUI


struct AvatarChooseView: View {
    @State private var currentIndex:Int = 0
    @AppStorage("curren_main_color") var currentMainColor = "color_main_01"
    @AppStorage("user_avator") var userAvatar = "user_header_01"
    @AppStorage("user_avator_base64") var avatorBase64 = ""
    @Environment(\.presentationMode) var presentationMode
    @AppStorage("login_token") var loginToken:String = ""
    
    struct AvatarModel:Identifiable {
        var id:String = UUID().uuidString
        var name:String
        var index:Int = 0
    }
    
    let columns:[GridItem] = [
        GridItem(.flexible(), spacing: 20, alignment: .center),
        GridItem(.flexible(), spacing: 20, alignment: .center),
        GridItem(.flexible(), spacing: 20, alignment: .center),
    ]
    
    let avatarList:[AvatarModel] = [
        AvatarModel(name: "user_header_01",index: 0),
        AvatarModel(name: "user_header_02",index: 1),
        AvatarModel(name: "user_header_03",index: 2),
        AvatarModel(name: "user_header_04",index: 3),
        AvatarModel(name: "user_header_05",index: 4),
        AvatarModel(name: "user_header_06",index: 5),
        AvatarModel(name: "user_header_07",index: 6),
        AvatarModel(name: "user_header_08",index: 7),
        AvatarModel(name: "user_header_09",index: 8),
        AvatarModel(name: "user_header_10",index: 9),
        AvatarModel(name: "user_header_11",index: 10),
        AvatarModel(name: "user_header_12",index: 11),
    ]
    
    var body: some View {
        VStack {
            HStack {
                Spacer()
                ShakeButton(label: Text("完成")) {
                    if !self.loginToken.isEmpty {
                        updataUserAvator()
                    } else {
                        userAvatar = self.avatarList[currentIndex].name
                        avatorBase64 = UIImage(named: userAvatar)?.base64 ?? ""
                        presentationMode.wrappedValue.dismiss()
                    }
                }.modifier(MainTextModifier(fontWeight: .bold))
                    .padding(.horizontal,20)
                    .padding(.vertical,10)
                
            }
            .padding(.top,10)
            .padding(.bottom,20)
            
            LazyVGrid(columns:columns, content: {
                ForEach(avatarList) { item in
                    Image(item.name)
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .cornerRadius(10)
                        .onTapGesture {
//                            userAvatar = item.name
//                            avatorBase64 = UIImage(named: userAvatar)?.base64 ?? ""
                            
                            currentIndex = item.index
                        }.overlay(alignment: .topTrailing) {
                            if currentIndex == item.index {
                                Image(systemName: "checkmark.circle.fill")
                                    .resizable()
                                    .frame(width: 24,height: 24)
                                    .foregroundColor(Color(currentMainColor))
                                    .offset(x: -6, y: 6)
                            }
                        }.overlay(alignment: .center) {
                            if currentIndex == item.index {
                                RoundedRectangle(cornerRadius: 10)
                                    .stroke(Color(currentMainColor), lineWidth: 4)
                            }
                        }
                }
            }).padding(.horizontal,20)
            Spacer()
        }
        
    }
    
    
    private func updataUserAvator() {
        let sendData = [
            "image":UIImage(named: self.avatarList[currentIndex].name)?.base64 ?? ""
        ]
        YWNetworkTool.share().requestJson(url: "info/editNNUserImage", parameters: sendData) { res in
            DPrint(res)
            userAvatar = self.avatarList[currentIndex].name
            avatorBase64 = UIImage(named: userAvatar)?.base64 ?? ""
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
                presentationMode.wrappedValue.dismiss()
            }
        } responseFailBlock: { error, code in
            YWToast.show(text: "上传头像失败，请稍后重试")
            
            presentationMode.wrappedValue.dismiss()
        }

    }

}

